package com.mylib.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.mylib.libcore.R;
import com.mylib.libcore.utils.ToastUtil;
import com.mylib.libcore.utils.UIUtils;

/**
 * @author Administrator
 * @date 2019/12/20-9:38
 * @description CustomScoreBar-自定义评分条
 **/
public class CustomScoreBar extends LinearLayout {
    private Context mContext;
    private View mView;
    private LinearLayout linearLayout;

    public CustomScoreBar(Context context) {
        this(context, null);
    }

    public CustomScoreBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomScoreBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }




    private void init(Context context, AttributeSet attrs) {
        mContext = context;
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = inflater.inflate(R.layout.custom_scorebar_layout, this, true);
        linearLayout = (LinearLayout) mView.findViewById(R.id.ll_score_bar);
    }

    /**
     * 必须初始化CustomScoreBar
     * score 分数
     */
    public void initCustomScoreBar(int score) {
        linearLayout.removeAllViews();
        int tenNumber = score/10;
        int bitNumber = score%10;
        Log.d("initCustomScoreBar",tenNumber+"---"+bitNumber);
        for (int i=0;i<10;i++) {
            //实例化一个LinearLayout
            View  item_scorebar_star =  View.inflate(mContext, R.layout.item_scorebar_star,null);
            ImageView iv_score = item_scorebar_star.findViewById(R.id.iv_score);
            if(i<tenNumber){
                iv_score.setBackground(mContext.getResources().getDrawable(R.drawable.img_heart_10));
            }
            if(i==tenNumber){
                switch (bitNumber){
                    case 1:
                        iv_score.setBackground(mContext.getResources().getDrawable(R.drawable.img_heart_1));
                        break;
                    case 2:
                        iv_score.setBackground(mContext.getResources().getDrawable(R.drawable.img_heart_2));
                        break;
                    case 3:
                        iv_score.setBackground(mContext.getResources().getDrawable(R.drawable.img_heart_3));
                        break;
                    case 4:
                        iv_score.setBackground(mContext.getResources().getDrawable(R.drawable.img_heart_4));
                        break;
                    case 5:
                        iv_score.setBackground(mContext.getResources().getDrawable(R.drawable.img_heart_5));
                        break;
                    case 6:
                        iv_score.setBackground(mContext.getResources().getDrawable(R.drawable.img_heart_6));
                        break;
                    case 7:
                        iv_score.setBackground(mContext.getResources().getDrawable(R.drawable.img_heart_7));
                        break;
                    case 8:
                        iv_score.setBackground(mContext.getResources().getDrawable(R.drawable.img_heart_8));
                        break;
                    case 9:
                        iv_score.setBackground(mContext.getResources().getDrawable(R.drawable.img_heart_9));
                        break;
                }
            }
            if(i>tenNumber){
                iv_score.setBackground(mContext.getResources().getDrawable(R.drawable.img_heart_0));
            }

            linearLayout.addView(item_scorebar_star);
        }
    }

}
